package cn.zhangxin.pethome.system.systemdictionarytype.controller;

import cn.zhangxin.pethome.system.systemdictionarydetail.domain.Systemdictionarydetail;
import cn.zhangxin.pethome.system.systemdictionarytype.domain.Systemdictionarytype;
import cn.zhangxin.pethome.system.systemdictionarytype.query.SystemdictionarytypeQuery;
import cn.zhangxin.pethome.system.systemdictionarytype.service.ISystemdictionarytypeService;
import cn.zhangxin.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/syst")
@Api(value = "品牌的API",description="品牌相关的CRUD功能")
public class SystemdictionarytypeController {
    @Autowired
    private ISystemdictionarytypeService systemdictionarytypeService;

    @GetMapping("/list")
    @ApiOperation(value = "获取列表" )
    public List<Systemdictionarytype> findAll(){
        return systemdictionarytypeService.findAll();
    }

    @PostMapping("/pagelist")
    @ApiOperation(value = "分页查询" )
    public AjaxResult findQuery(@RequestBody SystemdictionarytypeQuery query){
        System.out.println(query) ;
        try {
            List<Systemdictionarytype> byQuery = systemdictionarytypeService.findByQuery(query);
            Long count = systemdictionarytypeService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    public AjaxResult delete(@PathVariable Long id){
        try {
            systemdictionarytypeService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败，还有该类型的所属商品");
    }

    @PutMapping
    @ApiOperation(value = "保存" )
    public AjaxResult save(@RequestBody Systemdictionarytype systemdictionarytype){
        if(systemdictionarytype.getId()==null){
            try {
                systemdictionarytypeService.add(systemdictionarytype);
                return AjaxResult.success().setMsg("添加成功");
            }catch (Exception e){
                e.printStackTrace();
            }
            return AjaxResult.error().setMsg("添加失败");
        }else {
            try {
                systemdictionarytypeService.update(systemdictionarytype);
                return AjaxResult.success().setMsg("修改成功");
            }catch (Exception e){
                e.printStackTrace();
            }
            return AjaxResult.error().setMsg("修改失败");
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询子类" )
    public AjaxResult findBySystemdictionarydetail(@PathVariable Long id){
        try {
            List<Systemdictionarydetail> detail = systemdictionarytypeService.findBySystemdictionarydetail(id);
            Long count = systemdictionarytypeService.findBySystemdictionarydetailCount(id);
            return AjaxResult.success().setData(detail).setTotal(count).setPid(id);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
}
